import web
import buildjs
import database

web.config.debug = False

urls = (
	'/login',		'login',
	'/',			'Main',
	'/teste',		'teste',
	'/teste2',		'teste2',
	'/bin/getDomains',	'getDomains'
)
  
app = web.application(urls, locals())

session = web.session.Session(app, web.session.DiskStore('sessions'))

#render = web.template.render('tpl')
   
#class Main(object):
#	def GET(self):
#        	return render.main()

class login:
	def GET(self):
		return """
<form name="teste" action="login" method="post">
	Usuario: <input type="text" name="user"><br />
	Senha: <input type="password" name="pass"><br />
	<input type="submit" value="Logar">
</form>"""

	def POST(self):
		usuario = web.input(user="anonymous")
		print usuario.user
		if usuario.user == "joao":
			session.usuario = "joao"
			return "<script language='Javascript'>this.location.href='/';</script>"

class Main:
	def GET(self):
		print session.get('usuario')

		if session.get('usuario') == "joao":
			return "Dae Joao"
		else:
			return "<script language='Javascript'>this.location.href='/login';</script>"
		

class teste:
	def GET(self):
		r  = buildjs.makeHeader()
		r += buildjs.makeExtJsHeader()
		r += buildjs.makeWindowLogin("windowLogin","Authentication",300,160)
		r += buildjs.makeExtJsBottom()
		return r

class teste2:
	def GET(self):
		r  = buildjs.makeHeader()
		r += buildjs.makeExtJsHeader()
		r += buildjs.makeEnvDomains()
		r += buildjs.makeEnvPolicies()
		r += buildjs.makeViewPort("admin")
		r += buildjs.makeExtJsBottom()
		return r

class getDomain:
	def GET(self):
		conn = database.db.connect()
		data = conn.execute("select * from domain")



if __name__ == '__main__':
	app.run()
